package com.umpay.privi.dao.on.sys.impl;

import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.umpay.common.persist.AbstractDao;
import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.common.persist.ibatis.exception.SQLErrorCodeSQLExceptionTranslator;
import com.umpay.privi.dao.EntityDaoImpl;
import com.umpay.privi.dao.on.sys.RolePriviDao;
import com.umpay.privi.model.sys.RolePrivi;


/** 
 * desc:角色权限Dao实现类
 * <p>创建人：Ding Zhe 创建日期：May 6, 2013 </p>
 * @version V1.0  
 */
@SuppressWarnings("unchecked")
public class RolePriviDaoImpl extends EntityDaoImpl<RolePrivi> implements RolePriviDao {

	private static Logger log = LoggerFactory.getLogger(RolePriviDaoImpl.class);
	
	public RolePrivi get(Integer roleId ,Integer privilegeId )throws DataAccessException{
		Map<String, Object> kMap = new Hashtable<String, Object>();
		kMap.put(RolePrivi.ROLEID, roleId);
		kMap.put(RolePrivi.PRIVILEGEID, privilegeId);
		
		try {
			return (RolePrivi)getSqlMapClientTemplate().getSqlMapClient().queryForObject("RolePrivi.GetMap", kMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	public List<RolePrivi> findPriviListByRoleId(Map<String, Object> paraMap)throws DataAccessException{
		try {
			return (List<RolePrivi>)getSqlMapClient().queryForList("RolePrivi.GetMap", paraMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}

	
	public Integer updateByRoleId(Map<String, Object> paraMap)throws DataAccessException{
		try {
			return getSqlMapClient().update("RolePrivi.UpdatebyRoleId", paraMap);
		} catch (SQLException e) {
			throw new SQLErrorCodeSQLExceptionTranslator(AbstractDao.DB_TYPE_DB2).translate(e.getMessage(), e);
		}
	}
	
	public void insertBatch(final List<RolePrivi> rolePrivileges,final Integer roleId)
			throws DataAccessException {
		if (null == rolePrivileges || rolePrivileges.size() == 0) {
			log.error("角色权限关系为空！");
			return;
		}
		SqlMapClientCallback callback = new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor sqlMapExecutor)
					throws SQLException {
				int counter = 0;
				sqlMapExecutor.startBatch();
				for (RolePrivi rolePrivi : rolePrivileges) {
					rolePrivi.setRoleId(roleId);
					sqlMapExecutor.insert("RolePrivi.Insert", rolePrivi);
					counter ++;
					if (counter % 1000 == 0) {
						//设置一个计数器每隔1000条提交一次
						sqlMapExecutor.executeBatch();
						sqlMapExecutor.startBatch();
					}
				}
				sqlMapExecutor.executeBatch();
				return null;
			}
		};
		this.getSqlMapClientTemplate().execute(callback);
	}
	/**
	 * 
	 * desc:批量insert或update
	 * <p>创建人：liuguangjun , May 18, 2013 9:10:54 PM</p>
	 * @param clazz
	 * @param index
	 * @return
	 */
	public void batchInsertOrUpdate(final List<RolePrivi> objList)throws DataAccessException {
		if (null == objList || objList.size() == 0) {
			return;
		}
		
		SqlMapClientCallback callback = new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor sqlMapExecutor)
					throws SQLException {
				int counter = 0;
				sqlMapExecutor.startBatch();
				for (RolePrivi rolePrivi : objList) {
					RolePrivi resObj=(RolePrivi)sqlMapExecutor.queryForObject("RolePrivi.GetMap",rolePrivi);//get
					if(resObj==null){
						sqlMapExecutor.insert("RolePrivi.Insert", rolePrivi);//insert
					}else{
						sqlMapExecutor.insert("RolePrivi.Update", rolePrivi);//update
					}
					counter++;
					if (counter % 1000 == 0) {
						// 设置一个计数器每隔1000条提交一次
						sqlMapExecutor.executeBatch();
						sqlMapExecutor.startBatch();
					}
				}
				sqlMapExecutor.executeBatch();
				return null;
			}
		};
		this.getSqlMapClientTemplate().execute(callback);
	}
	
	
}



